/**
 * 610书窝小程序端 JavaScript
 */

document.addEventListener('DOMContentLoaded', function () {
  // 初始化轮播图
  initCarousel();

  // 初始化倒计时
  initCountdown();
});

/**
 * 轮播图功能
 */
function initCarousel() {
  const slides = document.querySelectorAll('.mp-carousel-slide');
  const indicators = document.querySelectorAll('.mp-indicator');
  let currentSlide = 0;
  let slideInterval;

  // 显示指定幻灯片
  function showSlide(index) {
    // 隐藏所有幻灯片
    slides.forEach(slide => {
      slide.classList.remove('active');
    });

    // 重置所有指示器
    indicators.forEach(indicator => {
      indicator.classList.remove('active');
    });

    // 显示当前幻灯片和指示器
    slides[index].classList.add('active');
    indicators[index].classList.add('active');
    currentSlide = index;
  }

  // 显示下一张幻灯片
  function nextSlide() {
    currentSlide = (currentSlide + 1) % slides.length;
    showSlide(currentSlide);
  }

  // 显示上一张幻灯片
  function prevSlide() {
    currentSlide = (currentSlide - 1 + slides.length) % slides.length;
    showSlide(currentSlide);
  }

  // 设置自动轮播
  function startAutoSlide() {
    slideInterval = setInterval(nextSlide, 3000); // 每3秒切换一次
  }

  // 停止自动轮播
  function stopAutoSlide() {
    clearInterval(slideInterval);
  }

  // 绑定指示器点击事件
  indicators.forEach((indicator, index) => {
    indicator.addEventListener('click', () => {
      showSlide(index);
      stopAutoSlide();
      startAutoSlide(); // 点击后重新开始自动轮播
    });
  });

  // 触摸控制
  const carousel = document.querySelector('.mp-carousel');
  let startX, endX;
  const minSwipeDistance = 50;

  carousel.addEventListener('touchstart', (e) => {
    startX = e.touches[0].clientX;
    stopAutoSlide();
  });

  carousel.addEventListener('touchmove', (e) => {
    endX = e.touches[0].clientX;
  });

  carousel.addEventListener('touchend', () => {
    if (!endX) return;

    const distance = endX - startX;

    if (Math.abs(distance) > minSwipeDistance) {
      if (distance > 0) {
        prevSlide(); // 右滑，显示上一张
      } else {
        nextSlide(); // 左滑，显示下一张
      }
    }

    startX = null;
    endX = null;
    startAutoSlide();
  });

  // 初始化轮播
  showSlide(0);
  startAutoSlide();
}

/**
 * 倒计时功能
 */
function initCountdown() {
  // 设置目标日期（当前日期 + 15天）
  const now = new Date();
  const targetDate = new Date(now);
  targetDate.setDate(now.getDate() + 15);
  targetDate.setHours(21, 18, 14, 0); // 21时18分14秒

  const daysElement = document.querySelector('.mp-countdown .mp-time-box:nth-child(1) .mp-time');
  const hoursElement = document.querySelector('.mp-countdown .mp-time-box:nth-child(2) .mp-time');
  const minutesElement = document.querySelector('.mp-countdown .mp-time-box:nth-child(3) .mp-time');
  const secondsElement = document.querySelector('.mp-countdown .mp-time-box:nth-child(4) .mp-time');

  function updateCountdown() {
    const now = new Date();
    const distance = targetDate - now;

    if (distance <= 0) {
      // 倒计时结束
      clearInterval(countdownInterval);
      daysElement.textContent = '0';
      hoursElement.textContent = '0';
      minutesElement.textContent = '0';
      secondsElement.textContent = '0';
      return;
    }

    // 计算时间差
    const days = Math.floor(distance / (1000 * 60 * 60 * 24));
    const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
    const seconds = Math.floor((distance % (1000 * 60)) / 1000);

    // 更新页面
    daysElement.textContent = days;
    hoursElement.textContent = hours;
    minutesElement.textContent = minutes;
    secondsElement.textContent = seconds;
  }

  // 初始更新一次
  updateCountdown();

  // 每秒更新一次
  const countdownInterval = setInterval(updateCountdown, 1000);
}

/**
 * 图书卡片悬停效果
 */
const bookCards = document.querySelectorAll('.mp-book-card');
bookCards.forEach(card => {
  card.addEventListener('click', () => {
    // 跳转到书籍详情页面
    // window.location.href = './pages/shop/detail.html';

    // 由于这是演示，暂时使用alert提示
    alert('点击了书籍卡片，将跳转到书籍详情页面');
  });
});

/**
 * 底部Tab点击事件
 */
const tabItems = document.querySelectorAll('.mp-tab-item');
tabItems.forEach(tab => {
  tab.addEventListener('click', function () {
    // 如果不是当前活跃的tab，才进行处理
    if (!this.classList.contains('active')) {
      // 实际应用中，这里会跳转到对应页面
      // 当前是演示，所以不添加实际跳转逻辑
    }
  });
}); 